package com.amat0urheu.service.cloudService.impl;

import com.amat0urheu.dao.CloudMapper;
import com.amat0urheu.dao.ImageMapper;
import com.amat0urheu.pojo.CloudPojo.ImagePojo;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.ImageService;
import com.amat0urheu.utils.CloudUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImageServiceImpl implements ImageService {
@Autowired
CloudMapper cloudMapper;

    @Value("${marsctf.ip}")
    private String ip;
    @Autowired
    ImageMapper imageMapper;
    @Override
    public ReturnRes getAllImages(Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<ImagePojo> images = imageMapper.getAllImages(ip);
        if (images != null) {
            return new ReturnRes(true, new PageInfo<>(images), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public void refreshImages( ) {
        List<ImagePojo> images= CloudUtils.getImages(cloudMapper.getOpenstackByopenstackIP(ip));

        List<ImagePojo> allImages = imageMapper.getAllImages(ip);

        Map<Integer,Integer> map=new HashMap<>();
        List<Integer>ids=new ArrayList<>();
        for (int i = 0; i <images.size() ; i++) {
            for (int j = 0; j < allImages.size(); j++) {
                if( images.get(i).getImageName().equals(allImages.get(j).getImageName())){
                    images.get(i).setId(allImages.get(j).getId());
                   images.get(i).setFlagNumber(allImages.get(j).getFlagNumber());
                   images.get(i).setInstanceType(allImages.get(j).getInstanceType());
                }
            }
            if(images.get(i).getId()==null){
                Integer maxId = imageMapper.getMaxId();
                images.get(i).setId(maxId+1);
            }
            ids.add(images.get(i).getId());
        }
        int i = imageMapper.deleteNotIds(ids);


        imageMapper.dropImages( ip);
        for (ImagePojo image:images) {
            imageMapper.inserImage(image);
        }
    }

    @Override
    public ReturnRes CreateFlavor() {
        return null;
    }

    @Override
    public ReturnRes removeFlavor() {
        return null;
    }

    @Override
    public ReturnRes getAllMachines() {
       List<ImagePojo> machines= imageMapper.getAllMachine(ip);
        if (machines != null) {
            return new ReturnRes(true, machines, "查询成功");
        }
        return new ReturnRes(false, "查询失败");

    }

    @Override
    public ReturnRes updateImage(ImagePojo image) {
        int count = imageMapper.updateImage(image);
        if(count>0){
            return new ReturnRes(true, "修改成功");
        }
        return new ReturnRes(false, "修改失败");
    }
}
